package com.yict.csms.resourceplan.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.BaseDao;
import com.yict.csms.resourceplan.dao.IBoxDao;
import com.yict.csms.resourceplan.entity.Box;

@Repository("boxDao")
public class BoxDaoImpl extends BaseDao<Box, Long> implements IBoxDao {

	@Override
	public String getMaxImportId(String date) {
		String result = "";
		String queryString = "select max(t.importId) from Box t where 1=1 and t.importId like :date ";
		try {
			Query query = this.getSession().createQuery(queryString);
			query.setParameter("date", date+'%');
			List<String> list = query.list();
			if (list != null && list.size() > 0) {
				result = list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public boolean findBoxByBoxNum(String boxNum) {
		boolean flag = false;
		String queryString = "from Box t where 1=1 and t.boxNum = :boxNum";
		try {
			Query query = this.getSession().createQuery(queryString);
			query.setParameter("boxNum", boxNum);
			List<String> list = query.list();
			if(list == null || list.size() == 0) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	@Override
	public boolean findBoxCLineByBoxNum(String boxNum) {
		boolean flag = false;
		String queryString = "from BoxContractor t, BoxContractorLine b where 1=1 and t.bcId = b.bcId and t.status = '未派更' and b.boxNum = :boxNum";
		try {
			Query query = this.getSession().createQuery(queryString);
			query.setParameter("boxNum", boxNum);
			List<String> list = query.list();
			if(list == null || list.size() == 0) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	@Override
	public boolean trans() {
/*		String queryString = "insert into Box(boxNum, importId, shipCompanyCode, yardPosition, getOrderNum, resGetDate, boxShape," +
				" createDate, updateDate, createPerson, updatePerson) select b.boxNum, b.importId, b.shipCompanyCode, b.yardPosition," +
				" b.getOrderNum, b.resGetDate, b.boxShape, b.createDate, b.updateDate, b.createPerson, b.updatePerson from BoxTemp b" +
				" where b.boxNum not in (select t.boxNum from Box t)";
		this.getSession().createQuery(queryString).executeUpdate();
		String queryString3 = "update Box t set(t.boxNum, t.importId, t.shipCompanyCode, t.yardPosition, t.getOrderNum, t.resGetDate, t.boxShape," +
			" t.createDate, t.updateDate, t.createPerson, t.updatePerson) = (select b.boxNum, b.importId, b.shipCompanyCode, b.yardPosition," +
			" b.getOrderNum, b.resGetDate, b.boxShape, b.createDate, b.updateDate, b.createPerson, b.updatePerson from BoxTemp b " +
			"where t.boxNum = b.boxNum) where b.boxNum in (select k.boxNum from Box k)";
		this.getSession().createQuery(queryString3).executeUpdate();
		String queryString2 = "delete from BoxTemp where 1=1";*/
		this.getSession().createSQLQuery("{call CSMS_SP_UpdateBox()}").executeUpdate();
		return false;
	}

	@Override
	public void deleteTemp() {
		String queryString = "delete from BoxTemp where 1=1";
		this.getSession().createQuery(queryString).executeUpdate();
	}
	
}
